package com.xpgk.mvc.dept;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.xpgk.dto.RenderBean;
import com.xpgk.dto.ZtreeNode;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.user.User;

/**
 * 部门管理
 */
public class DepartmentController extends BaseController {

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(DepartmentController.class);

	private DepartmentService departmentService;

	private String pIds; // 上级部门ids
	private String name; // 部门名称
	private int orderId; // 部门排序号
	private int level;// 部门级别
	private String pBh;

	/**
	 * tree首页
	 */
	public void index() {
		setAttr("curUserDept", Department.dao.findById(getCUserdeptIds()));
		render("/department/department.html");
	}

	/**
	 * 部门树形图节点数据
	 */
	public void treeData() {
		List<ZtreeNode> nodeList = departmentService.childNodeData(getCxt(), ids, getCUserdeptIds());
		renderJson(nodeList);
	}

	/**
	 * 部门用户列表
	 */
	public void deptUserList() {
		splitPage.getQueryParam().put("organizationids", getCUserOrganIds());
		String json = paging(splitPage, User.sqlId_splitPageSelect, User.sqlId_splitPageFrom);
		renderJson(json);
	}
	
	/**
	 * 部门用户列表
	 */
	public void personnelList() {
		splitPage.getQueryParam().put(User.column_organizationids, getCUserOrganIds());
		splitPage.getQueryParam().put(User.column_departmentids, getCUserdeptIds());
		if(splitPage.getQueryParam().get("bh")!=null){
			String bhString = (String) splitPage.getQueryParam().get("bh");
			String[] bhs = bhString.split("\\.");
//			for (int i = 0; i < bhs.length; i++) {  
//				bhs[i] = bhs[i]+"%";
//				}
			splitPage.getQueryParam().put("bhs", bhs);
		}
		
		String json = paging(splitPage, User.sqlId_splitPageSelect, User.sqlId_splitPageFrom2);
		renderJson(json);
	}

	/**
	 * 保存
	 */
	public void addDept() {
		ids = departmentService.save(pIds, name, orderId, level, pBh, getCUserOrganIds());
		renderText(ids);
	}

	/**
	 * 更新
	 */
	public void updateDept() {
		departmentService.update(ids, name);
		renderText(ids);
	}

	/**
	 * 删除
	 */
	public void deleteDept() {
		String result = departmentService.delete(ids);
		renderText(result);
	}
	
	/**
	 * 跳转至添加用户
	 */
	public void toAddUser(){
		setAttr("dept", Department.dao.findById(ids));
		render("addUser.html");
	}
	
	/**
	 * 设置岗位tree
	 */
	public void setStation(){
		List<ZtreeNode> nodeList = departmentService.stationChildNodeData(getCxt(), ids, getCUserStationIds());
		renderJson(nodeList);
	}
	
	/**
	 * 添加用户
	 */
	@Before(DeptValidator.class)
	public void addUser(){
		User user = getModel(User.class);
		RenderBean renderBean = departmentService.addUser(user, getCUserOrganIds());
		renderJson(renderBean);
	}
	
	/**
	 * 跳转至编辑用户
	 */
	public void toUpUser(){
		
		setAttr("user", User.dao.findById(ids));
		
		setAttr("organ", Organization.dao.findById(getCUserOrganIds()));
		
		if(ids!=null &&!ids.equals("")){
			if(ids.equals(getCUserIds())){
				setAttr("flag", "1");
			}else{
				setAttr("flag", "0");
			}
		}
		render("upUser.html");
	}
	
	/**
	 * 编辑用户
	 */
	@Before(DeptValidator.class)
	public void upUser(){
		User user = getModel(User.class);
		RenderBean renderBean = departmentService.upUser(user);
		renderJson(renderBean);
	}
}
